# src/main.py
import uvicorn
import logging
from fastapi import FastAPI

# Relative imports from the current package (src)
from .database.connection import init_db_pool
from .api import patients as patients_router
from .api import appointments as appointments_router
from .api import medical_records as medical_records_router
from .api import prescriptions as prescriptions_router
from .api import reports as reports_router

# Configure root logger
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)

# Create FastAPI app
app = FastAPI(
    title="Hospital Management System API",
    description=(
        "API for managing patients, staff, appointments, medical records, "
        "prescriptions and reports. Uses raw SQL (psycopg2)."
    ),
    version="1.0.0"
)

# Include routers with path prefixes
app.include_router(patients_router.router, prefix="/patients", tags=["patients"])
app.include_router(appointments_router.router, prefix="/appointments", tags=["appointments"])
app.include_router(medical_records_router.router, prefix="/medical-records", tags=["medical_records"])
app.include_router(prescriptions_router.router, prefix="/prescriptions", tags=["prescriptions"])
app.include_router(reports_router.router, prefix="/reports", tags=["reports"])

# Startup event to initialize database pool
@app.on_event("startup")
def startup_event():
    init_db_pool(minconn=1, maxconn=10)
    logging.info("Database connection pool initialized.")

# Root endpoint
@app.get("/")
def read_root():
    return {"status": "ok", "service": "Hospital Management System API"}

# Main entry point
if __name__ == "__main__":
    uvicorn.run("src.main:app", host="0.0.0.0", port=8000, reload=True)
